package com.wenguo.common.util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.wenguo.Constants;
import com.wenguo.bussines.model.Log;
import com.wenguo.bussines.model.User;
import com.wenguo.common.util.URLHelper.PageInfo;

public final class BeansUtil {
	
	private BeansUtil() {
		
	}
	
	public static boolean checkContains(String uri) {
		return uri.startsWith(Constants.securityUrl.RESOURCES)
			||uri.toLowerCase().endsWith(Constants.securityUrl.REGISTER)
			||uri.endsWith(Constants.system.SECURITY_CODE)
			||uri.endsWith(Constants.securityUrl.IS_USER_EXIST)
			||uri.equals(Constants.securityUrl.INDEX)
			||uri.endsWith(Constants.securityUrl.LOGIN)
			||uri.endsWith(Constants.securityUrl.LOGOUT);
	}
	
	public static User getLoginUser(HttpServletRequest request) {
		return (User)request.getSession().getAttribute(Constants.system.USER);
	}
	
	public static boolean checkSession(HttpServletRequest request) {
		User user = getLoginUser(request);
		return user == null ? false : true;
	}
	
	public static boolean checkQueryUriAndString(HttpServletRequest request) {
		User user = getLoginUser(request);
		if (null != user && Constants.group.ADMIN.equals(user.getGroup().getId().toString())) {
			return true;
		} else {
			//check uri of admin
			String uri = URLHelper.getURI(request);
			if (uri.startsWith(Constants.securityUrl.ADMIN)) {
				return false;
			}
			//check query string
			Map<String, String> map = URLHelper.getParamsMap(request);
			if (null != map && !map.isEmpty()) {
				String uid = map.get(Constants.system.UID);
				if ("".equals(uid) || user.getId().equals(uid)) {
					return true;
				}
				String id = map.get(Constants.system.ID);
				if ("".equals(id) || user.getId().equals(id)){
					return true;
				}
			} else {
				return true;
			}
		}
		return false;
	}
	
	public static Log getLogInfo(User user, HttpServletRequest request) {
		Log log = new Log();
		String[] s = RequestUtil.getLogRequest(request);
		log.setIp(s[0]);
		log.setDate(s[1]);
		log.setUser(user);
		return log;
	}
	
	private static Map<Object, Object> getQueryMap(PageInfo pageInfo,  HttpServletRequest request, Boolean isAddUserId) {
		Map<Object, Object> map;
		if (null == pageInfo.getQueryMap()) {
			map = new HashMap<Object, Object>();
		} else {
			Map<String, String> tmp_map = pageInfo.getQueryMap();
			if (isAddUserId && null == tmp_map.get(Constants.system.UID)) {
				tmp_map.put(Constants.system.UID, getLoginUser(request).getId());
			}
			map = new HashMap<Object, Object>(tmp_map);
		}
		if (null == map.get(Constants.system.PAGE_START)) {
			map = addPageInfo2Map(map, pageInfo);
		}
		return map;
	}
	private static Map<Object, Object> addPageInfo2Map(Map<Object, Object> map, PageInfo pageInfo) {
		if (null == map) {
			return null;
		} else {
			int pageNo = pageInfo.getPageNo();
			int pageSize = pageInfo.getPageSize();
			map.put(Constants.system.PAGE_START, (pageNo - 1) * pageSize);
			map.put(Constants.system.PAGE_SIZE, pageSize);
			return map;
		}
	}
	
	public static Map<Object, Object> getLogQueryParams(PageInfo pageInfo, HttpServletRequest request) {
		
		 return getQueryMap(pageInfo, request, true);
	}
	
	public static Map<Object, Object> getUserQueryParams(PageInfo pageInfo, HttpServletRequest request) {
		
		return getQueryMap(pageInfo, request, false);
	}
	
	public static boolean checkSecurityCode(HttpServletRequest request, String scode) {
		String code = (String)request.getSession().getAttribute(Constants.system.SECURITY_CODE);
		return MD5.code(scode.toLowerCase()).equals(code);
	}

}
